package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Question;
import com.itheima.service.QuestionItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author TJH
 * @date 2019-12-15 8:45
 */

@RestController
@RequestMapping("/questionItem")
public class QuestionItemController {

    @Reference
    private QuestionItemService questionItemService;

    /**
     * 添加问题
     * @param question
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Question question) {
        try{
           questionItemService.add(question);
           return new Result(true,"问题添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }

    /**
     * 修改问题
     * @param question
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Question question){
        try{
            questionItemService.edit(question);
            return new Result(true,"修改问题成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    /**
     * 根据题目id查找问题
     * @param id
     * @return
     */
    @RequestMapping("/findQuestionItemById")
    public Result findQuestionItemById(Integer id){
        try{
            Question questionItem = questionItemService.findQuestionItemById(id);
            return new Result(true,"查询成功",questionItem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult= questionItemService.findPage(queryPageBean);
            return new Result(true,"分页查询成功",pageResult);
        }catch(Exception e){
            e.printStackTrace();
        }
        return new Result(true,"分页查询失败");
    }

    /**
     * 根据id删除问题
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            questionItemService.delete(id);
            return new Result(true,"删除题目成功");
        }catch(Exception e){
            e.printStackTrace();
        }
        return new Result(false,"删除题目失败");
    }
}
